// patrolnpc.txt
// Creature attacks anything it hates nearby, and spends the rest of its time patrolling a path.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - Number of path to follow.
//	 Cell 1 - If 0, no patrol. If 1, walks to end of path, waits a little, and returns home.
//      If 2, goes to end of path and just stops.
//   Cell 2 - Node if talked to. 0 means no conversation
//   Cell 3,4 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
// Cell 5 - If 1, can take control

begincreaturescript;

variables;

short i,target;
short did_adjust = 0;
short control = 0;

body;

beginstate INIT_STATE;
	set_aggression(ME,8);
	set_new_abil(ME,20);
	if (gf(2,13) == 1)
		set_aggression(ME,6);
		
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(3) != 0) || (get_memory_cell(4) != 0))
		inc_flag(get_memory_cell(3),get_memory_cell(4),1);
break;

beginstate START_STATE;
	if ((get_attitude(ME) < 10) &&((char_ok(38) == FALSE) || (get_attitude(38) >= 10))) {
		if (my_number() == 35)
			set_attitude(ME,10);
		if (my_number() == 36)
			set_attitude(ME,10);
		if (my_number() == 37)
			set_attitude(ME,10);
		}
		
	if ((did_adjust == 0) && (gf(2,13) == 1)) {
		did_adjust = 1;
		set_aggression(ME,6);
		}
		
	if (get_foe_target(ME,8,0)) {
		if ((get_memory_cell(5) == 0) || (gf(2,13) < 2))
			do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (get_memory_cell(1) != 0) {
			if ((my_dist_from_start() >= 2) && (get_memory_cell(1) != 2)) {
				if (get_ran(1,1,100) < 40) 
					return_to_start(ME,1);
				}
			if ((get_ran(1,1,100) < 30) || (get_memory_cell(1) == 2)) {
				follow_path(ME,get_memory_cell(0),0);
				set_state(4);
				}
			}
	if ((control > 0) && (get_attitude(ME) < 10)) {
		if (dist_to_pc() > 12) {
			print_str("A guard creation runs to catch up with you.");
			telep_char_to_char(ME,pc_num(),FALSE);
			}
			else maintain_dist_to_char(ME,pc_num(),2);
		}
		
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	
	if ((control == 0) && (gf(2,13) > 1) && (get_attitude(ME) >= 10) && (get_memory_cell(5) > 0)) {
		control = 1;
		set_attitude(ME,3);
		set_foe_target(ME,-1);
		print_str_color("You are able to seize control of one of the guard creations.",2);
		if (gf(3,28) == 0) {
			sf(3,28,1);
			begin_talk_mode(13);
			}
		set_state(START_STATE);
		
		}
	if ((control > 0) && (dist_to_pc() >= 10) && (get_attitude(ME) < 10)) {
		set_foe_target(ME,-1);
		maintain_dist_to_char(ME,pc_num(),2);
		set_state(START_STATE);
		}
		
	do_attack();
break;

beginstate 4; // patrol
	if (get_foe_target(ME,8,0)) {
		if ((get_memory_cell(5) == 0) || (gf(2,13) < 2))
			do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (follow_path(ME,get_memory_cell(0),0) == TRUE)
		set_state(START_STATE);
break;

beginstate TALKING_STATE;
	if (control > 0) {
		print_str("You examine the creation. It is still under your control.");
		}
		else begin_talk_mode(11);	
break;